$('.delete').click(function(){
	var id = $(this).parents('.item').data('id');
	$('#myModal').attr('token',id);
	$('#myModal').modal();
});
$('.user_manager .confirm').click(function(){
	var id = $(this).parents('.modal').attr('token');
	var data = {"user.id": id};
	$.post("user_delete.html", data, function(){
		$('#myModal').modal('toggle');
		window.location.reload();
	})
});

$('.equipment_manager .confirm').click(function(){
	var id = $(this).parents('.modal').attr('token');
	var data = {"equipment.id": id};
	$.post("equipment_delete.html", data, function(){
		$('#myModal').modal('toggle');
		window.location.reload();
	})
});

$('.workflow_manager .confirm').click(function(){
	var id = $(this).parents('.modal').attr('token');
	var data = {"id": id};
	$.post("workflow_delete.html", data, function(){
		$('#myModal').modal('toggle');
		window.location.reload();
	})
});

$('.perworkflow_manager .confirm').click(function(){
	var id = $(this).parents('.modal').attr('token');
	var data = {"id": id};
	$.post("workflow_perDelete.html", data, function(){
		$('#myModal').modal('toggle');
		window.location.reload();
	})
});

$('#addEquipment').click(function(){
	$('#list').hide('slow');
	$('#equipment').show('slow',function(){
		var form = $(this).find('form');
		$(form).find("input[name='img']").attr('required',true);
		$("input").each(function(){
			if(this.type=='hidden'){
				$(this).remove();
			}else{
				$(this).val('');
			}
			
		});
		$('img.img').attr('src','');
		$('select option:first-child').attr('selected',true);
	});
	
});

$('#info').click(function(){
	$('#list').hide('slow');
	$('#forInfo').show('slow');
	
});
$('#pwd').click(function(){
	$('#list').hide('slow');
	$('#forPWD').show('slow');
	
});


$("#forInfo .close").click(function(){
	$('#list').show('slow');
	$('#forInfo').hide('slow');
});

$("#forPWD .close").click(function(){
	$('#list').show('slow');
	$('#forPWD').hide('slow');
});

$('#forPWD .submit').click(function(){
	var pwd1 = $('.first').val();
	var pwd2 = $('.second').val();
	if(pwd1 == pwd2){
		var old = $('#forPWD .old').val();
		var newPWD = $('#forPWD .first').val();
		var data = {"user.password": old, "newPWD": newPWD};
		$.post('user_updatePWD.html', data, function(result){
			if(result == "success"){
				window.location.reload();
			}else{
				$('#forPWD form').addClass('error');
			}
		});
	}else{
		$('#forPWD form').addClass('info');
	}
});
$('#equipment .submit').click(function(){
	$('#equipment').attr('action','equipment_add.html');
	$('#equipment').submit(function(result){
		console.log(result);
	});
})
$("#addAddress").click(function(){
	$('#list').hide('slow');
	$('#address').show('slow');
	$('.invalid').removeClass('invalid');
	$("input[type='text']").each(function(){
		$(this).val('');
	});
	$("input[type='hidden']").each(function(){
		$(this).remove();
	});
});

$("#address .close").click(function(){
	$('#list').show('slow');
	$('#address').hide('slow');
});

$("#equipment .close").click(function(){
	$('#list').show('slow');
	$('#equipment').hide('slow');
});
$(".user_manager #list .item .editDetail").click(function(){
	var items = $(this).parents('.item').find('.editable');
	var form = $('form');
	$('.invalid').removeClass('invalid');
	$(items).each(function(){
		var input = $("input[name='"+$(this).attr('name')+"']");
		if(input.length > 0){
			$(input).val($(this).text());
			if($(this).attr('name').indexOf('account') > -1){
				$(input).attr('old', $(this).text());
			}
		}else{
			var temp = document.createElement("input");
			temp.type = "hidden";
			temp.name = $(this).attr('name');
			temp.value = $(this).text();
			$(form).append(temp);
		}
		
	});
	$('#list').hide('slow');
	$('#address').show('slow');
});
$(".equipment_manager #list .item .editDetail").click(function(){
	var item = $(this).parents('.item');
	var form = $('form');
	var temp = document.createElement("input");
	temp.type = "hidden";
	temp.name =  "equipment.id";
	temp.value = $(item).find(".editable[name='equipment.id']").text();
	$(form).append(temp);
	$(form).find("input[name='equipment.name']").val($(item).find(".editable[name='equipment.name']").text());
	$(form).find("select[name='equipment.type']").val($(item).find(".editable[name='equipment.type']").text());
	$(form).find("input[name='equipment.status']").val($(item).find(".editable[name='equipment.status']").text());
	
	$(form).find("img.img").attr("src",($(item).find(".editable[name='equipment.img']").data('img')));
	$(form).find("input[name='img']").attr('required',false);
	$('#list').hide('slow');
	$('#equipment').show('slow');
});
$("#addWorkflow").click(function(){
	$('#list').hide('slow');
	$('#installForm').show('slow');

	setTimeout(function(){
		map.clearOverlays();    //æ¸…é™¤åœ°å›¾ä¸Šæ‰€æœ‰è¦†ç›–ç‰©
	    var point = new BMap.Point(16.385495,39.871171);
	    
	    map.enableScrollWheelZoom(true);
	    var marker = new BMap.Marker(point);// åˆ›å»ºæ ‡æ³¨
	    map.centerAndZoom(point,12);
	    map.addOverlay(marker);    //æ·»åŠ æ ‡æ³¨
	    marker.enableDragging();           // å�¯æ‹–æ‹½
		var p = marker.getPosition();       //èŽ·å�–markerçš„ä½�ç½®
		document.getElementById("lng").value=p.lng ;
		document.getElementById("lat").value=p.lat; 
	    marker.addEventListener("mouseup",getAttr);
		function getAttr(){
			var p = marker.getPosition();       //èŽ·å�–markerçš„ä½�ç½®
			document.getElementById("lng").value=p.lng ;
			document.getElementById("lat").value=p.lat; 
		}
	    var ac = new BMap.Autocomplete(    //å»ºç«‹ä¸€ä¸ªè‡ªåŠ¨å®Œæˆ�çš„å¯¹è±¡
	        {"input" : "suggestId"
	        ,"location" : map
	    });

	    ac.addEventListener("onhighlight", function(e) {  //é¼ æ ‡æ”¾åœ¨ä¸‹æ‹‰åˆ—è¡¨ä¸Šçš„äº‹ä»¶
	    var str = "";
	        var _value = e.fromitem.value;
	        var value = "";
	        if (e.fromitem.index > -1) {
	            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	        }    
	        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
	        
	        value = "";
	        if (e.toitem.index > -1) {
	            _value = e.toitem.value;
	            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	        }    
	        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
	        G("searchResultPanel").innerHTML = str;
	    });

	    var myValue;
	    ac.addEventListener("onconfirm", function(e) {    //é¼ æ ‡ç‚¹å‡»ä¸‹æ‹‰åˆ—è¡¨å�Žçš„äº‹ä»¶
	    var _value = e.item.value;
	        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
	        
	        setPlace();
	    });

	    function setPlace(){
	        map.clearOverlays();    //æ¸…é™¤åœ°å›¾ä¸Šæ‰€æœ‰è¦†ç›–ç‰©
	        function myFun(){
	            var pp = local.getResults().getPoi(0).point;    //èŽ·å�–ç¬¬ä¸€ä¸ªæ™ºèƒ½æ�œç´¢çš„ç»“æžœ
	            var marker = new BMap.Marker(pp);// åˆ›å»ºæ ‡æ³¨
	            map.centerAndZoom(pp, 18);
	            map.addOverlay(marker);    //æ·»åŠ æ ‡æ³¨
	            marker.enableDragging();           // å�¯æ‹–æ‹½
				var p = marker.getPosition();       //èŽ·å�–markerçš„ä½�ç½®
				document.getElementById("lng").value=p.lng ;
				document.getElementById("lat").value=p.lat; 
	            marker.addEventListener("mouseup",getAttr);
				function getAttr(){
					var p = marker.getPosition();       //èŽ·å�–markerçš„ä½�ç½®
					document.getElementById("lng").value=p.lng ;
					document.getElementById("lat").value=p.lat; 
				}
	        }
	        var local = new BMap.LocalSearch(map, { //æ™ºèƒ½æ�œç´¢
	          onSearchComplete: myFun
	        });
	        local.search(myValue);
	    }


	},1000);
});

$("#list .item .editInstall").click(function(){
	var item = $(this).parents('.item');
	var form = $('form');
	var temp = document.createElement("input");
	temp.type = "hidden";
	temp.name =  "workflow.id";
	temp.value = $(item).find(".editable[name='workflow.id']").text();
	$(form).append(temp);

	$(form).find("input[name='workflow.primaryKey']").val($(item).find(".editable[name='workflow.primaryKey']").text());
	$(form).find("input[name='workflow.phone']").val($(item).find(".editable[name='workflow.phone']").text());
	$(form).find("input[name='time']").val($(item).find(".editable[name='workflow.time']").text());
	var userid = $(item).find(".editable[name='workflow.user.id']").text();
	$("select[name='userid'] option").each(function(){
	if($(this).attr('value') == $(item).find(".editable[name='workflow.user.id']").text()){
		 $(this).attr('selected',true);
	}
	});
	$("select[name='equipmentid'] option").each(function(){
		if($(this).attr('value') == $(item).find(".editable[name='workflow.equipment.id']").text()){
			 $(this).attr('selected',true);
		}
		});
	$("select[name='workflow.status'] option").each(function(){
		if($(this).attr('value') == $(item).find(".editable[name='workflow.status']").text()){
			 $(this).attr('selected',true);
		}
		});
	$(form).find("input[name='workflow.lng']").val($(item).find(".editable[name='workflow.lng']").text());
	$(form).find("input[name='workflow.lat']").val($(item).find(".editable[name='workflow.lat']").text());
	$('#list').hide('slow');
	$('#installForm').show('slow');
	setTimeout(function(){
		map.clearOverlays();    //æ¸…é™¤åœ°å›¾ä¸Šæ‰€æœ‰è¦†ç›–ç‰©
	    var point = new BMap.Point($(item).find(".editable[name='workflow.lng']").text(),$(item).find(".editable[name='workflow.lat']").text());
	    
	    map.enableScrollWheelZoom(true);
	    var marker = new BMap.Marker(point);// åˆ›å»ºæ ‡æ³¨
	    map.centerAndZoom(point,12);
	    map.addOverlay(marker);    //æ·»åŠ æ ‡æ³¨
	    marker.enableDragging();           // å�¯æ‹–æ‹½
		var p = marker.getPosition();       //èŽ·å�–markerçš„ä½�ç½®
		document.getElementById("lng").value=p.lng ;
		document.getElementById("lat").value=p.lat; 
	    marker.addEventListener("mouseup",getAttr);
		function getAttr(){
			var p = marker.getPosition();       //èŽ·å�–markerçš„ä½�ç½®
			document.getElementById("lng").value=p.lng ;
			document.getElementById("lat").value=p.lat; 
		}
	    var ac = new BMap.Autocomplete(    //å»ºç«‹ä¸€ä¸ªè‡ªåŠ¨å®Œæˆ�çš„å¯¹è±¡
	        {"input" : "suggestId"
	        ,"location" : map
	    });

	    ac.addEventListener("onhighlight", function(e) {  //é¼ æ ‡æ”¾åœ¨ä¸‹æ‹‰åˆ—è¡¨ä¸Šçš„äº‹ä»¶
	    var str = "";
	        var _value = e.fromitem.value;
	        var value = "";
	        if (e.fromitem.index > -1) {
	            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	        }    
	        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
	        
	        value = "";
	        if (e.toitem.index > -1) {
	            _value = e.toitem.value;
	            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	        }    
	        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
	        G("searchResultPanel").innerHTML = str;
	    });

	    var myValue;
	    ac.addEventListener("onconfirm", function(e) {    //é¼ æ ‡ç‚¹å‡»ä¸‹æ‹‰åˆ—è¡¨å�Žçš„äº‹ä»¶
	    var _value = e.item.value;
	        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	        G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
	        
	        setPlace();
	    });

	    function setPlace(){
	        map.clearOverlays();    //æ¸…é™¤åœ°å›¾ä¸Šæ‰€æœ‰è¦†ç›–ç‰©
	        function myFun(){
	            var pp = local.getResults().getPoi(0).point;    //èŽ·å�–ç¬¬ä¸€ä¸ªæ™ºèƒ½æ�œç´¢çš„ç»“æžœ
	            var marker = new BMap.Marker(pp);// åˆ›å»ºæ ‡æ³¨
	            map.centerAndZoom(pp, 18);
	            map.addOverlay(marker);    //æ·»åŠ æ ‡æ³¨
	            marker.enableDragging();           // å�¯æ‹–æ‹½
				var p = marker.getPosition();       //èŽ·å�–markerçš„ä½�ç½®
				document.getElementById("lng").value=p.lng ;
				document.getElementById("lat").value=p.lat; 
	            marker.addEventListener("mouseup",getAttr);
				function getAttr(){
					var p = marker.getPosition();       //èŽ·å�–markerçš„ä½�ç½®
					document.getElementById("lng").value=p.lng ;
					document.getElementById("lat").value=p.lat; 
				}
	        }
	        var local = new BMap.LocalSearch(map, { //æ™ºèƒ½æ�œç´¢
	          onSearchComplete: myFun
	        });
	        local.search(myValue);
	    }

	},1000);
	setTimeout(function(){
		$(form).find("input[name='workflow.primaryKey']").val($(item).find(".editable[name='workflow.primaryKey']").text());
		},1100);
	 
});


$(document).on('click',"#address .submit",function(){
	var invalid = $('.invalid');
	if($(invalid).length > 0){
		return;
	}else{
		$(this).parents('form').submit();
	}
})
$(document).on('blur','#account',function(){
	var form = $(this).parents('form');
	if($(this).attr('old') != '' && $(this).attr('old') != $(this).val()){
		var account = $(this).val();
		var data = {"user.account": account};
		$.post("user_checkAccount.html", data, function(result){
			if("invalid" == result){
				$('.user_manager form .glyphicon').addClass('invalid');
			}else{
				$('.user_manager form .glyphicon').removeClass('invalid');
			}
		})
	}else{
		$('.user_manager form .glyphicon').removeClass('invalid');
	}
});

$('.install').click(function(){
	$('#list').hide('slow');
	$('#installForm').show('slow');
	$("input[name=equipmentid]").val($(this).parent().siblings(".hidden").text());	
});

$("#installForm .close").click(function(){
	$('#list').show('slow');
	$("#installForm").hide('slow');
});


$(document).on('click',"#installForm .submit",function(){

	$("#installForm form").submit();
});


//ç™¾åº¦åœ°å›¾APIåŠŸèƒ½
function G(id) {
    return document.getElementById(id);
}